from manim import *



class work5(MovingCameraScene):
    def construct(self):
        self.camera.background_color = BLACK  # 设置背景颜色
        self.camera.frame_width = 100  # 设置边框宽度
        self.camera.frame_height = 60  # 设置边框高度
        self.camera.pixel_height = 1080  # 设置像素高度
        self.camera.pixel_width = 1920  # 设置像素宽度
        self.camera.center = ORIGIN  # 设置中心点位置
        self.camera.scale_factor = 1.0  # 设置缩放因子
        #设置横线
        for i in range(6*2+1):       
            dot1=Dot([-50,5*(i-6),0]).set_opacity(0.5)
            dot2=Dot([50,5*(i-6),0]).set_opacity(0.5)
            if i==6:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
                
            else:
                line1=Line(dot1,dot2).set_color(WHITE).set_opacity(0.5)
               
            self.add(dot1,dot2,line1)
        #设置竖线
        for i in range(10*2+1):        
            dot3=Dot([(i-10)*5,-30,0]).set_opacity(0.5)
            dot4=Dot([(i-10)*5,30,0]).set_opacity(0.5)
            if i==10:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
                
            else:
                line2=Line(dot3,dot4).set_color(WHITE).set_opacity(0.5)
            self.add(dot3,dot4,line2)
        #设置三个点
        dot1 = Dot(radius=1, color=RED)  
        dot1.move_to([-47.5,27.5,0]) 
        dot2 = Dot(radius=1, color=YELLOW)  
        dot2.move_to([-42.5,27.5,0])  
        dot3 = Dot(radius=1, color=GREEN)  
        dot3.move_to([-37.5,27.5,0])
         
         #题目出现
        text = Tex(r"\text{(2013)设函数}$f\left( x\right) =\begin{cases}\sin x,0\leq x <\pi \\2,\pi \leq x <2\pi \end{cases},F\left( x\right) =\int _{0}^{x}f\left( t\right) dt$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-10,20,0])
        '''
        text99 = Tex(r"$g\left( x\right) =\begin{cases}e^{x},0 <x\leq 1,\\0,x=0,\\-e^{-x},-1\leq x <0,\end{cases}$"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([30,20,0])
                   '''
        text100 = Tex(r"\text{则下列说法正确的是()}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30,12,0])
        text1 = Tex(
                   r"\text{(A)}$x=\pi $\text{是F(x)的跳跃间断点}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-24.5,7,0])
        text2 = Tex(
                    r"\text{(B)}$x=\pi $\text{是F(x)的可去间断点}}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-24.5,1,0])
        text3 = Tex(
                   r"\text{(C)}\text{F(x)在}$x=\pi $\text{处连续但不可导}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-24,-5,0])
        text4 = Tex(
                    r"\text{(D)}\text{F(x)在}$x=\pi $\text{可导}"
                   ,tex_template=TexTemplateLibrary.ctex).scale(5.0).move_to([-30.5,-11,0])

        self.play(Write(text),Write(text100),Write(text1),Write(text2),Write(text3),Write(text4),run_time=2)
        self.wait(0.1)
        #镜头拉回
          #分析文本
        #text100=MarkupText("<b><i>分析/解：</i></b>",color=PINK).scale(5.0).move_to([-38,12.5,0])
        
        self.play(FadeIn(dot1),run_time=0.1)
        self.play(FadeIn(dot2),run_time=0.1)
        self.play(FadeIn(dot3),run_time=0.1)
        #题目向上移动
        #self.play(text.animate().move_to([0,20,0]),text1.animate().move_to([-22,10,0]) ,text2.animate().move_to([-22,0,0]),text3.animate().move_to([-22,-10,0]),text4.animate().move_to([-22,-20,0]) )
        


        #画坐标轴

        axes = Axes(
            x_range=[-PI, 2*PI, PI],  # 设置 x 轴范围，从 -3π 到 3π，步长为 π/2
            y_range=[-1.5, 1.5, 0.5],     # 设置 y 轴范围，从 -1.5 到 1.5，步长为 0.5
            axis_config={"color": WHITE,"stroke_width": 30},  # 设置坐标轴的颜色
            x_length=40,  # 控制 x 轴长度
            y_length=20 ,   # 控制 y 轴长度
        )
        axes.move_to([20,-10,0])
        # 绘制 cos(x) 函数图像，负半轴部分为蓝色
        sin_graph_left = axes.plot(lambda x: np.sin(x), color=BLUE,stroke_width=30, x_range=[0, PI])

        # 添加坐标系和函数图像到场景中
        #self.add(axes, cos_graph_left, cos_graph_right)
        dot1=Dot([27,3.3,0])
        dot2=Dot([39,3.3,0])
        line=Line(dot1,dot2).set_color(BLUE)
        line.set_stroke(width=30)  # 设置宽度为 30
        # 添加标题
        title = Text("f(x)",color=GREEN, font_size=240).next_to(axes, UP,buff=1)
        self.add(title)
        #下划线
        horizontal_line = axes.plot(
            lambda x: 2,  # 水平线的 y 值（根据你的坐标轴位置调整）
            x_range=[27, 39],  # 根据原 Line 的 x 范围设置
                color=BLUE,
            stroke_width=30
            )       
        
        # 显示图像
        self.play(Create(axes),  Write(title),Create(sin_graph_left),Create(horizontal_line),Create(line))
        self.wait()

        dot4=Dot([13.2,10,0])
        dot5=Dot([13.2,-20,0])
        line2=Line(dot4,dot5).set_color(YELLOW)
        line2.set_stroke(width=30)  # 设置宽度为 30
        
        self.play(Create(line2))
        # 定义直线函数
        def constant_line(x):
            return 0  # 这里的常数是示例值，你可以根据实际需要调整

        # 创建直线图像
        line_graph = axes.plot(constant_line, x_range=[-PI, 4*PI], color=PURE_RED, stroke_width=20)
        area5 = axes.get_area(horizontal_line, [PI, 3*PI/2], bounded_graph=line_graph, color=RED, opacity=0.5)
        area3 = axes.get_area(sin_graph_left, [0, PI/2], bounded_graph=line_graph, color=GREEN, opacity=0.5)
        area4 = axes.get_area(sin_graph_left, [PI/2, PI], bounded_graph=line_graph, color=GREEN, opacity=0.5)
        # 也要添加到场景中
        rectangle=Rectangle(color=BLUE,fill_opacity=0.3,width=21,height=5
                            ,stroke_color=BLUE,stroke_width=20)
        rectangle.move_to([15, 20, 0])
        self.play(Create(rectangle))
        self.play(line2.animate().scale(1).shift(RIGHT*7),
                   
                  Create(area3))
        self.play(line2.animate().scale(1).shift(RIGHT*6.7),
                   
                  Create(area4))
        
        rectangle1=Rectangle(color=RED,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=RED,stroke_width=0).move_to([-24.5,7,0])
        rectangle2=Rectangle(color=RED,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=RED,stroke_width=0).move_to([-24.5,1,0])
       
        self.play(
            rectangle1.animate.stretch(40.0,dim=0),
            rectangle2.animate.stretch(40.0,dim=0),
        )
        self.play(line2.animate().scale(1).shift(RIGHT*6.5),
                   
                  Create(area5))
        rectangle3=Rectangle(color=GREEN,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=GREEN,stroke_width=0).move_to([-24.5,-5,0])
        rectangle4=Rectangle(color=RED,fill_opacity=0.5,width=1,height=2.8
                            ,stroke_color=RED,stroke_width=0).move_to([-24.5,-11,0])
       
        self.play(
           
            rectangle3.animate.stretch(40.0,dim=0),
            rectangle4.animate.stretch(40.0,dim=0),
        )
        self.wait()